package com.time.company.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.time.company.app.MyApplication;
import com.time.company.app.MyManager;
import com.time.company.db.ViewModelCreator;
import com.time.company.servermodel.BasicBean;
import com.time.company.utils.ToastUtils;
import com.time.company.webserver.listener.ResponseCallback;
import com.umeng.analytics.MobclickAgent;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.rest.Response;

/**
 * Dialog Activity 基类
 * Created by Bing on 2016/8/23.
 * E-mail is tx16bing@163.com
 */
public abstract class BaseDialogActivity extends FragmentActivity
        implements ResponseCallback<BasicBean> {

    public MyApplication application;
    public ViewModelCreator mVMcreator;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        application = MyApplication.getInstance();
        mVMcreator = ViewModelCreator.getInstance(application);
        // 把当前的Activity加入管理器
        MyManager.getInstance().addActivity(this);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
    }

    /**
     * 当点击外部的时候隐藏软键盘
     * @param ev event
     * @return 返回值
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        /*if (this.getCurrentFocus() != null) {
            InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }*/
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            // 独占本次事件，不做任何处理
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(toString());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(toString());
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 取消指定的请求
        NoHttp.getRequestQueueInstance().cancelBySign(toString());
    }

    @Override
    protected void onDestroy() {
        MyManager.getInstance().removeActivity(this);
        super.onDestroy();
    }

    // 初始化控件id
    protected <T extends View> T findView(int resId) {
        return (T) (findViewById(resId));
    }

    @Override
    public void onSucceed(int what, Response<BasicBean> response) {
        // 请求成功
    }

    @Override
    public void onFailed(int what, Object tag, Exception exception, long networkMillis) {
        // 请求失败
        if (exception != null){
            ToastUtils.showShort(exception.getMessage());
        }
    }

    public void showKeyboard(boolean isShow) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) {
            return;
        }

        if (isShow) {
            if (getCurrentFocus() == null) {
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            } else {
                imm.showSoftInput(getCurrentFocus(), 0);
            }
        } else {
            if (getCurrentFocus() != null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }

        }
    }
}
